/**
 * 微信授权服务
 * 处理微信登录授权、获取用户信息等功能
 */

import { ref } from 'vue';
import { getAccessToken } from '@/utils/auth';

export const useWechat = () => {
  // 定义微信授权状态
  const authState = ref({
    isLogining: false,
    authDenied: false,
  });

  /**
   * 获取微信登录凭证code
   * @returns Promise 返回登录凭证code
   */
  const getLoginCode = () => {
    return new Promise((resolve, reject) => {
      // #ifdef MP-WEIXIN
      uni.login({
        provider: 'weixin',
        success: (res) => {
          if (res.code) {
            resolve(res.code);
          } else {
            reject(new Error('获取微信登录凭证失败'));
          }
        },
        fail: (err) => {
          reject(err);
        },
      });
      // #endif

      // #ifndef MP-WEIXIN
      reject(new Error('当前环境不支持微信登录'));
      // #endif
    });
  };

  /**
   * 获取微信用户手机号
   */
  const getPhoneNumber = (e) => {
    return new Promise((resolve, reject) => {
      authState.value.isLogining = true;

      if (e.detail.errMsg !== 'getPhoneNumber:ok') {
        authState.value.isLogining = false;
        authState.value.authDenied = true;
        reject(new Error('用户拒绝授权'));
        return;
      }

      getLoginCode()
        .then((code) => {
          // #ifdef MP-WEIXIN
          resolve({
            code,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
          });
          // #endif

          // #ifndef MP-WEIXIN
          resolve({
            code,
            ...(e.detail.code ? { phoneCode: e.detail.code } : {}),
          });
          // #endif
        })
        .catch((err) => {
          reject(err);
        })
        .finally(() => {
          authState.value.isLogining = false;
        });
    });
  };

  /**
   * 检查会话有效性
   * @returns Promise 返回会话是否有效
   */
  const checkSession = () => {
    return new Promise((resolve) => {
      const token = getAccessToken();

      if (!token) {
        resolve(false);
        return;
      }

      uni.request({
        url: '/api/v1/auth/check-session',
        method: 'GET',
        header: {
          Authorization: `Bearer ${token}`,
        },
        success: (res) => {
          if (res.statusCode === 200 && res.data.valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        },
        fail: () => {
          resolve(false);
        },
      });
    });
  };

  /**
   * 获取用户头像昵称
   */
  const getUserProfile = () => {
    return new Promise((resolve, reject) => {
      // #ifdef MP-WEIXIN
      uni.getUserProfile({
        desc: '用于完善用户资料',
        success: (res) => {
          resolve(res.userInfo);
        },
        fail: (err) => {
          reject(err);
        },
      });
      // #endif

      // #ifndef MP-WEIXIN
      reject(new Error('当前环境不支持获取用户信息'));
      // #endif
    });
  };

  return {
    authState,
    getLoginCode,
    getPhoneNumber,
    checkSession,
    getUserProfile,
  };
};
